package com.zoweunion.mechanic.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.RoleService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController{
	
	@Autowired
	private RoleService roleService;

	
	/**
	 * 查询所有角色
	 */
	@PostMapping("/getAll")
	public ResponseBean getAll(@RequestBody String str){
		logBefore(logger, "角色管理_查询所有");
		Map<String, Object> reqMap = JSONObject.parseObject(str, JSONObject.class);
		List<Map<String, Object>> roles = roleService.getAll(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", roles);
	}
	
	
	/**
	 * 查询所有权限
	 */
	@PostMapping("/getAllPermissions")
	public ResponseBean getAllPermissions(HttpServletRequest request) throws MyException {
		logBefore(logger, "查询所有权限");
		User currentUser = getCurrentUser(request);
		List<Map<String, Object>> permissions=roleService.getAllPermissions(currentUser);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",permissions);
	}
	/**
	 * 查询父权限权限
	 */
	@PostMapping("/getParentPermissions")
	public ResponseBean getParentPermissions() {
		logBefore(logger, "查询所有父权限");
		List<Map<String, Object>> permissions=roleService.getParentPermissions();
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",permissions);
	}
	/**
	 * 查询所有权限
	 */
	@PostMapping("/getChildrenPermissions")
	public ResponseBean getChildrenPermissions(@RequestBody String str) {
		logBefore(logger, "根据parent_id查询子权限");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		List<Map<String, Object>> permissions=roleService.getChildrenPermissions(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",permissions);
	}
	
	/**
	 * 根据角色id,服务商id查询所有权限
	 */
	@PostMapping("/getAllPermissionsByRole")
	public ResponseBean getAllPermissionsByRole(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "根据角色查询权限");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id",getCurrentUser(request).getS_id());
		List<Map<String, Object>> permissions=roleService.getAllPermissionsByRole(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",permissions);
	}
	
	
	
	/**
	 * 禁用角色
	 * @throws MyException 
	 */
	@PostMapping("/ban")
	public ResponseBean ban(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "角色管理_禁用");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("status",2);
		reqMap.put("update_user",getCurrentUser(request).getId());
		roleService.update(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	
	
	/**
	 * 启用角色
	 */
	@PostMapping("/recover")
	public ResponseBean recover(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "角色管理_启用");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("status",1);
		reqMap.put("update_user",getCurrentUser(request).getId());
		roleService.update(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	
	/**
	 * 编辑角色 (修改角色对应的权限)
	 */
	@PostMapping("/updateRolePermission")
	public ResponseBean updateRolePermission(@RequestBody String str,HttpServletRequest request) throws MyException{
		logBefore(logger, "角色管理_编辑权限");
		Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);
		User currentUser = getCurrentUser(request);
		reqMap.put("update_user", currentUser.getId());
		reqMap.put("create_user", currentUser.getId());
		reqMap.put("s_id", currentUser.getS_id());
		roleService.updateRole_permission(reqMap);
		logAfter(logger);
		return new ResponseBean(Constants.CODE_SUCCESS,"操作成功",null);
	}
	
}
